<?php
/**
 * 修改保留价  初始化数据用 请勿做他用,擅自执行
 * Created by PhpStorm.
 * User: angl
 * Date: 15/12/23
 * Time: 上午9:27
 */
die("The end");
date_default_timezone_set ( "Asia/Shanghai" );
set_time_limit(0);
if (isset($_SERVER ['REQUEST_URI'])) {
    die('error');
}
if (!defined('INC_ROOT_PATH')) {
    define('INC_ROOT_PATH', dirname(dirname(__FILE__)) . '/');
}
require_once (INC_ROOT_PATH . '/inc/Config.php');
require_once (INC_ROOT_PATH . '/inc/MysqliDb.php');

$db = new MysqliDb (MYSQLDB, MYSQLDB_USER, MYSQLDB_PWD, MYSQLDB_DATABASE);
$db->setPrefix ('au_');

$cars = $db->get('cars');

echo "----------------------Start-------------------------\n";
//修复车源保留价历史记录
if ($db->count > 0)
    echo "Start repair car reserve price history:\n";
    foreach($cars as $car){
        //更新车源保留价历史
        $owner = $db->where('owner_id', $car['owner_id'])->getOne('car_owner');
        $checker = $db->where('emp_id', $owner['checker_id'])->getOne('employee');
        $reserve_price_history = array(
            array(
                'price' => $car['reserve_price'],
                'name' => $checker['real_name'],
                'id' => $owner['checker_id'],
                'time' => $car['posttime']
            )
        );

        $data = array();
        $data['reserve_price_history'] = json_encode($reserve_price_history, JSON_UNESCAPED_UNICODE);
        $res = $db->where('car_id',$car['car_id'])->update('cars', $data);
        if($res)
            echo "[". date("Y-m-d H:i:s") ."]" . $car['car_id'] . ": reserve price history were update\n";
        else
            echo "[". date("Y-m-d H:i:s") ."]" . 'update failed: ' . $db->getLastError() . "\n";
    }
    echo "Repair car reserve price history finished!\n";

//更新拍单保留价
$orders = $db->get ("order");

if ($db->count > 0)
    echo "Start update order reserve price \n";
    foreach ($orders as $order) {
        //获取保留价
        $car = $db->where('car_id',$order['car_id'])->getOne('cars');
        //更新拍单保留价
        $data = array();
        $data['reserve_price'] = $car['reserve_price'];

        $result = $db->where('order_id', $order['order_id'])->update('order', $data);
        //处理结果，输出日志
        if ($result)
            echo "[". date("Y-m-d H:i:s") ."] " . $order['order_id'] . ' - ' . $car['reserve_price'] . ' were updated'."\n";
        else
            echo "[". date("Y-m-d H:i:s") ."] " . 'update failed: ' . $db->getLastError() . "\n";
    }
    echo "Update order reserve price finished \n";

echo "-------------------The end-------------------\n";
